﻿using Reference.Data;
using Reference.WebRole.ServiceContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace Reference.WebRole
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class ReferenceService : IReferenceService
    {

        public List<DataContracts.CodeGroup> GetGroups()
        {
            ReferenceEntities referenceContext = new ReferenceEntities();
            var groups = from g in referenceContext.CodeGroup
                         select new DataContracts.CodeGroup
                         {
                             Name = g.Name,
                             GroupId = g.GroupId
                         };
            return groups.ToList();
        }


        public List<DataContracts.CodeSnippetLight> GetRecentSnippets(string groupId)
        {
            int gid = 0;
            if (Int32.TryParse(groupId, out gid))
            {
                ReferenceEntities referenceContext = new ReferenceEntities();
                var snippets = (from g in referenceContext.CodeSnippet
                                where g.GroupId == gid
                                orderby g.AddedDate
                                select new DataContracts.CodeSnippetLight
                                {
                                    Title = g.Title,
                                    AddedDate = g.AddedDate,
                                    Id = g.Id,
                                    Lang = g.Lang.Trim(),
                                    GroupId = gid
                                }).Take(5);
                return snippets.ToList();
            }
            return new List<DataContracts.CodeSnippetLight>();
        }


        public DataContracts.CodeSnippet GetSnippet(string groupId, string snippetId)
        {
            ReferenceEntities referenceContext = new ReferenceEntities();
            int gid = 0, sid = 0;
            if (Int32.TryParse(groupId, out gid) &&
                Int32.TryParse(snippetId, out sid))
            {
                string groupName = (from gp in referenceContext.CodeGroup
                                    where gp.GroupId == gid
                                    select gp.Name).First();
                var snippets = (from g in referenceContext.CodeSnippet
                                where g.GroupId == gid && g.Id == sid
                                orderby g.AddedDate
                                select new DataContracts.CodeSnippet
                                {
                                    Title = g.Title,
                                    AddedDate = g.AddedDate,
                                    Id = g.Id,
                                    Lang = g.Lang.Trim(),
                                    Notes = g.Notes,
                                    Code = g.Code,
                                    GroupName = groupName,
                                    Visitas = g.Visitas,
                                    GroupId = gid
                                }).Take(1);
                return snippets.ToList().Count > 0 ? snippets.ToList()[0] : null;
            }
            return null;

        }


        public List<Reference.WebRole.DataContracts.CodeSnippetLight> GetNextSnippets(string groupId, string lastSnippetId, string count)
        {
            ReferenceEntities referenceContext = new ReferenceEntities();
            int gid = 0, sid = 0, iCount = 0;
            if (Int32.TryParse(groupId, out gid) &&
                Int32.TryParse(lastSnippetId, out sid) &&
                Int32.TryParse(count, out iCount))
            {
                var snippets = (from g in referenceContext.CodeSnippet
                                where g.GroupId == gid && g.Id > sid
                                orderby g.AddedDate
                                select new DataContracts.CodeSnippetLight
                                {
                                    Title = g.Title,
                                    AddedDate = g.AddedDate,
                                    Id = g.Id,
                                    Lang = g.Lang.Trim(),
                                    GroupId = gid
                                }).Take(iCount);
                return snippets.ToList();
            }
            return new List<DataContracts.CodeSnippetLight>();
        }


        public List<DataContracts.CodeSnippetLight> GetSearchSnippets(string term, string word)
        {
            ReferenceEntities referenceContext = new ReferenceEntities();
            if (word.Length > 4)
            {
                var snippets = (from g in referenceContext.CodeSnippet
                                where g.Notes.Contains(word) || g.Title.Contains(word)
                                orderby g.AddedDate
                                select new DataContracts.CodeSnippetLight
                                {
                                    Title = g.Title,
                                    AddedDate = g.AddedDate,
                                    Id = g.Id,
                                    Lang = g.Lang.Trim(),
                                    GroupId = g.GroupId
                                }).Take(10);
                return snippets.ToList();
            }
            return new List<DataContracts.CodeSnippetLight>();
        }
    }
}
